package com.example.jdimust.main.ui.Dataquery;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.example.jdimust.util.ACache;
import com.example.jdimust.ledger.LedMes;
import com.example.jdimust.ledger.LedgaerActivity;
import com.example.jdimust.R;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;


@SuppressLint("ValidFragment")
public class DataqueryFragment extends Fragment {
    //查询

    static String loginmes;
    private View root, listitem;
    List<String> data = null;
    private List<LedMes> ledmestList = new ArrayList<>();
    private TextView led_hash;
    public final static int CONNECT_TIMEOUT = 60;
    public final static int READ_TIMEOUT = 100;
    public final static int WRITE_TIMEOUT = 60;
    public static final OkHttpClient client = new OkHttpClient.Builder()
            .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)//设置读取超时时间
            .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)//设置写的超时时间
            .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)//设置连接超时时间
            .build();

    @SuppressLint("ValidFragment")
    public DataqueryFragment(String res_in_all) {
        //此时的string就是我们传过来的值 123456
//        Log.d("TAG", res_in_all);
        loginmes = res_in_all;
    }

    //特别注意，千万不可忽略的一步，少了这个构造方法会报错
    public DataqueryFragment() {
        // Required empty public constructor
    }

    @SuppressLint({"ValidFragment", "SetTextI18n"})


    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.fragment_dashboard, container, false);
        ACache acache=ACache.get(getContext());//创建ACache组件
        TextView tv_name = (TextView) root.findViewById(R.id.tv_loginuser);
        Button bt_led = (Button) root.findViewById(R.id.lookchain);
        Button bt_con = (Button) root.findViewById(R.id.lookcon);
        listitem = inflater.inflate(R.layout.allled_item, container, false);
        int Idzdy = 10;
        tv_name.setId(Idzdy);
        tv_name.setText("当前登录用户:" + acache.getAsString("acaname"));

/*

        Gson gson = new Gson();
        LoginBean loginbean = gson.fromJson(loginmes, LoginBean.class);
//        System.out.println(LoginBean.getName());
//        System.out.println("------------------------------------------------------------\n"+tv_name.getId()+ "\n------------------------------------------------------------");
//        led_hash = listitem.findViewById(R.id.ledmes_lv_ledhash);


        String regetjson =  NetworkRequest.getaccount("/GetMessage/ledger","","");
        LedgerBean ledgerbean = gson.fromJson(regetjson,LedgerBean.class);

        String ledneme,ledhash,ledid,ledstate;
        for (LedgerBean.ResultDTO resultDTO : ledgerbean.getResult()) {
            ledhash = resultDTO.getLedgerHash();
            ledneme = resultDTO.getLedgerName();
            ledid = String.valueOf(resultDTO.getId());
            ledstate = resultDTO.getState();
//            allres = resultDTO.toString();
//            String mesche = "{\n" +
//                    "\"ledgerData\":"+ allres+"\n"+
//                    "}";
            String restate = null;

            JSONObject alledmesjson = null;
            try {
                alledmesjson = new JSONObject(regetjson);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            String allres = alledmesjson.optString("result", null);
                String mesche = "{\n" +
                            "\"ledgerData\":"+ allres+"\n"+
                            "}";

            try {
                restate = MessageCheck.mescheck("/ledger", mesche);
            } catch (IOException e) {
                e.printStackTrace();
            }
            LedMes ledmes = new LedMes(ledneme, ledhash, ledid, restate);
            ledmestList.add(ledmes);
            TextView tv_ledsta = (TextView) root.findViewById(R.id.ledmes_lv_ledsta);
            if (restate == "\"pass\""){
                tv_ledsta.setTextColor(Color.GREEN);
            }
        }



*/
/*
        getledMes();
        *//*


        LedMesAdapter adapter = new LedMesAdapter(getActivity(), R.layout.allled_item, ledmestList);
        ListView listView = (ListView) root.findViewById(R.id.lv_allled);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                TextView viewByLedId = (TextView) view.findViewById(R.id.ledmes_lv_ledid);
                TextView viewByLedHash = (TextView) view.findViewById(R.id.ledmes_lv_ledhash);
                TextView viewByLedName = (TextView) view.findViewById(R.id.ledmes_lv_ledname);
                System.out.println(viewByLedId.getText());
                String ledgerid = (String) viewByLedId.getText();
                String ledgerName = (String) viewByLedName.getText();
                String ledgerHash = (String) viewByLedHash.getText();
//                Intent intent = new Intent(getContext(), AccountActivity.class);
//                intent.putExtra("ledgerName", viewByLedName.getText());//参数：name、value
//                intent.putExtra("ledgerHash", viewByLedHash.getText());//参数：name、value
//                intent.putExtra("ledgerid", viewByLedId.getText());//参数：name、value

                acache.put("acaledgerid",ledgerid);
                acache.put("acaledgerHash",ledgerHash);
                acache.put("acaledgerName",ledgerName);
                startActivity(new Intent(getContext(), AccountActivity.class));
                Toast.makeText(getActivity(), "正在打开：" + viewByLedName.getText(), Toast.LENGTH_SHORT).show();
            }
        });
*/


        bt_led.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //里面写点击后想要实现的效果
                startActivity(new Intent(getContext(), LedgaerActivity.class));
                Toast.makeText(getContext(), "按钮被点击", Toast.LENGTH_SHORT).show();
                //这里是弹出一个消息---"按钮被点击"
            }
        });

        bt_con.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //里面写点击后想要实现的效果
                Toast.makeText(getContext(), "暂未开放！", Toast.LENGTH_SHORT).show();
                //这里是弹出一个消息---"按钮被点击"
            }
        });



        return root;
    }

//    private void getledMes() {
////        new Thread(new Runnable() {
////            @Override
////            public void run() {
//        try {
//            OkHttpClient client = new OkHttpClient();//创建OkHttpClient对象
//            Request request = new Request.Builder()
//                    .url("https://hzy.graduationproject.xyz/GetMessage/ledger")//请求接口。如果需要传参拼接到接口后面。
//                    .build();//创建Request 对象
//            Response response = null;
//            response = client.newCall(request).execute();//得到Response 对象
//            if (response.isSuccessful()) {
//                String getallled = response.body().string();
//                JSONObject alledmesjson = new JSONObject(getallled);
//                String allres = alledmesjson.optString("result", null);
//                JSONArray jsonArray = new JSONArray(allres);
////                System.out.println(allres);
//                for (int j = 0; j < jsonArray.length(); j++) {
//                    JSONObject jsonobj = jsonArray.getJSONObject(j);
//                    String ledgerName = jsonobj.optString("ledgerName", null);
//                    String ledgerHash = jsonobj.optString("ledgerHash", null);
//                    String state = jsonobj.optString("state", null);
//                    String id = jsonobj.optString("id", null);
//                    String mesche = "{\n" +
//                            "\"ledgerData\":"+ allres+"\n"+
//                            "}";
//                    System.out.println(mesche);
//                    String restate = MessageCheck.mescheck("/ledger", mesche);
////                    String callStr = MessageCheck.mescheck("/ledger", mesche);
//                    LedMes ledmes = new LedMes(ledgerName, ledgerHash, id, restate);
//                    ledmestList.add(ledmes);
//                }
////                Log.d(TAG, "flag" + msg);
//                //此时的代码执行在子线程，修改UI的操作请使用handler跳转到UI线程。
//            }
//        } catch (Exception e) {
////            e.printStackTrace();
//            System.out.println("somethings");
//        }
//    }
}
